﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.IO;
using System.Data;

namespace NJL.WebManage.Lines
{
    public partial class EditLines : BasePage
    {
        NJL.BLL.NJL_TRIP_LINE bll = new BLL.NJL_TRIP_LINE();
        NJL.Model.NJL_TRIP_LINE model;
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                model = bll.GetModel(Request.QueryString["id"].ToString());
                if (model != null)
                {
                    txtDescription.Text = model.SEO_DESCRIPTION;
                    txtKeyWords.Text = model.SEO_KEYWORDS;
                    txtTitle.Text = model.SEO_TITLE;
                    txtTag.Text = model.NEWS_TAG;
                    txtAttractionsTitle.Text = model.NEWS_TITLE;
                    txtAttractionFtitle.Text = model.NEWS_SUBJECT_TITLE;
                    txtContent.Text = model.NEWS_CONTECT;
                    if (model.IMAGE_CODE != "")
                    {
                        Image1.ImageUrl = GetImageUrl(model.IMAGE_CODE);
                    }
                    else
                    {
                        Image1.Visible = false;
                    }
                }
            }
        }
        protected void btnSubmit_Click(object sender, EventArgs e)
        {
            model = bll.GetModel(Request.QueryString["id"].ToString());
            model.SEO_DESCRIPTION = txtDescription.Text;
            model.SEO_KEYWORDS = txtKeyWords.Text;
            model.SEO_TITLE = txtTitle.Text;
            model.NEWS_TITLE = txtAttractionsTitle.Text;
            model.NEWS_SUBJECT_TITLE = txtAttractionFtitle.Text;
            model.NEWS_TAG = txtTag.Text;
            model.NEWS_CONTECT = txtContent.Text;
            model.ID = Request.QueryString["id"].ToString();
            model.UPDATE_DATE_TIME = DateTime.Now;
            model.UPDATE_USER_NAME = USER_NAME;
            model.UPDATE_USER_ID = USER_ID;
            if (bll.Update(model))
            {
                #region 标签处理
                NJL.BLL.NJL_CS_TAG bllTag = new BLL.NJL_CS_TAG();
                NJL.Model.NJL_CS_TAG modelTag = new Model.NJL_CS_TAG();
                char[] tagtxt1 = { ',', '，' };
                string[] tag = txtTag.Text.Trim().Split(tagtxt1);
                foreach (string strtag in tag)
                {
                    DataSet ds = bllTag.GetList("TAG='" + strtag + "' and NJL_SYS_BASE_ID='" + WEBSITE_CODE + "'");
                    if (ds.Tables[0].Rows.Count > 0)
                    {
                        modelTag = bllTag.GetModel(ds.Tables[0].Rows[0][0].ToString());
                        if (modelTag != null)
                        {
                            if (modelTag.USED_COUNT != null)
                            {
                                modelTag.USED_COUNT += 1;
                                if (bllTag.Update(modelTag))
                                {
                                }
                            }
                        }
                    }
                    else
                    {
                        if (strtag != "")
                        {
                            modelTag.ID = DateTime.Now.ToString("yyyyMMddHHmmssfff") + new Random(Guid.NewGuid().GetHashCode()).Next(100, 999).ToString();
                            modelTag.IS_SYS_TAG = 0;
                            modelTag.TAG = strtag;
                            modelTag.CREATE_DATE_TIME = DateTime.Now;
                            modelTag.NJL_SYS_BASE_ID = WEBSITE_CODE;
                            modelTag.SEARCH_COUNT = 0;
                            modelTag.TAG_TYPE = "3";  //线路是3
                            modelTag.USED_COUNT = 0;
                            bllTag.Add(modelTag);
                        }
                    }
                }
                #endregion 
                Royalsoft.Common.MessageBox.ShowAndRedirect(this, "更新成功", "EditLines.aspx?id=" + Request.QueryString["id"]+"&QQ="+Request.QueryString["QQ"]);
            }
        }
        protected void btnUpload_Click(object sender, EventArgs e)
        {
            if (FileUpload1.HasFile)
            {
                model = bll.GetModel(Request.QueryString["id"].ToString());
                if (model != null)
                {
                    DeleteImageByCode(model.IMAGE_CODE);   //删除文件
                    model.IMAGE_CODE = SaveUploadFile(Request.Files);
                    if (bll.Update(model))
                    {
                        Royalsoft.Common.MessageBox.Show(this, "上传成功");
                    }
                    if (model.IMAGE_CODE != "")
                    {
                        Image1.ImageUrl = GetImageUrl(model.IMAGE_CODE);
                        Image1.Visible = true;
                    }
                    else
                    {
                        Image1.Visible = false;
                    }

                }

            }
        }
    }
}